package cn.bob.service.impl;

import cn.bob.domain.MovieTicket;
import cn.bob.domain.UserMovie;
import cn.bob.dto.BuyNumDto;
import cn.bob.dto.SelectByMovieDto;
import cn.bob.exception.GlobalException;
import cn.bob.mapper.MovieTicketMapper;
import cn.bob.result.JsonResult;
import cn.bob.service.IMovieTicketService;
import cn.bob.service.IUserMovieService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.*;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bob
 * @since 2022-11-23
 */
@Service
public class MovieTicketServiceImpl extends ServiceImpl<MovieTicketMapper, MovieTicket> implements IMovieTicketService {
    @Autowired
    private IUserMovieService userMovieService;

    @Autowired
    private MovieTicketMapper movieTicketMapper;

    @Autowired
    private IMovieTicketService movieTicketService;
    /**
     * 电影票购买接口
     * @param buyNumDto
     */
    @Override
    public void buy(BuyNumDto buyNumDto) {
        if (buyNumDto.getNum()<=0){
            throw new GlobalException("购买的票数必须大于0!!");
        }
        //根据id查询数据库
        MovieTicket movieTicket = selectById(buyNumDto.getId());
        if (movieTicket==null){
            throw new GlobalException("没有此电影！！");
        }

        //判断库存是否足够
        if (movieTicket.getStock()<buyNumDto.getNum()){
            throw new GlobalException("本场次电影票数不够了!!");
        }

        //查询用户是否有这个电影票  通过中间表
        UserMovie userMovie = userMovieService.selectByUserAndMovie(buyNumDto.getId(), buyNumDto.getUserId());
        MovieTicket movie = movieTicketService.selectById(buyNumDto.getId());
        if (userMovie!=null&& buyNumDto.getId()==movie.getId()){
            userMovie.setCount(buyNumDto.getNum()+userMovie.getCount());
            userMovieService.updateById(userMovie);
        }
        else {
            userMovie = new UserMovie();
            userMovie.setUserId(buyNumDto.getUserId());
            userMovie.setMovieTicetId(movieTicket.getId());
            userMovie.setCount(buyNumDto.getNum());
            userMovieService.insert(userMovie);
        }

        //电影票减少
        movie.setStock(movie.getStock()- buyNumDto.getNum());
        updateById(movie);

    }

    /**
     * 根据电影的id查询是否过期
     * @param selectByMovieDto
     */
    @Override
    public JsonResult selectByMovie(SelectByMovieDto selectByMovieDto) {
        return getNotOverdue(selectByMovieDto);
    }

    private JsonResult getNotOverdue(SelectByMovieDto selectByMovieDto) {
        //查询电影票
        MovieTicket movieTicket = selectById(selectByMovieDto.getMovieId());
        if (movieTicket==null){
            throw new GlobalException("没有此电影！！");
        }
        //查询用户是否有这个电影票  通过中间表
        UserMovie userMovie = userMovieService.selectByUserAndMovie(selectByMovieDto.getMovieId(),selectByMovieDto.getUserId());
        if (userMovie==null){
            throw new GlobalException("该用户没有此电影票！！");
        }
        long newtime = new Date().getTime();
        long endtime= movieTicket.getEndTime().getTime();
        if (newtime>endtime){
            throw new GlobalException("电影票过期了不可用!!!");
        }
        return JsonResult.success("存在未过期!!");
    }

    //获取随机电影票一张
    @Override
    public JsonResult randomMovie(Long userId) {
        List<MovieTicket> movieTickets =movieTicketMapper.findAll();
        Random random = new Random();
        int i = random.nextInt(movieTickets.size());
        MovieTicket movieTicket = movieTickets.get(i);

        //添加到中间表中
        UserMovie userMovie = new UserMovie();
        userMovie.setUserId(userId);
        userMovie.setMovieTicetId(movieTicket.getId());
        userMovie.setCount(1);
        userMovieService.insert(userMovie);

        movieTicket.setStock(movieTicket.getStock()-1);
        updateById(movieTicket);
        return JsonResult.success("赠送成功");
    }

    @Override
    public JsonResult seeMovie(BuyNumDto buyNumDto) {
        //查询没有过期的票
        SelectByMovieDto selectByMovieDto = new SelectByMovieDto(buyNumDto.getId(), buyNumDto.getUserId());
        getNotOverdue(selectByMovieDto);


        //存在且未过期进行使用
        //根据userid与movieid进行消费
        UserMovie userMovie=userMovieService.selectByUserAndMovie(buyNumDto.getId(), buyNumDto.getUserId());
        if (buyNumDto.getNum()>userMovie.getCount()){
            throw new GlobalException("您的电电影票不足！！");
        }
        userMovie.setCount(userMovie.getCount()-buyNumDto.getNum());
        userMovieService.updateById(userMovie);
        return JsonResult.success("观看电影消费了"+buyNumDto.getNum()+"张票!!");
    }

}
